package edu.asu.sefcom.extract.profile.data;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.common.base.Splitter;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;

public class MicrosoftTranslate {

	static Logger logger = Logger.getLogger(MicrosoftTranslate.class);

	// public static void main(String[] args){translate("");}

	public static String translate(String toTransate) {
		Translate.setClientId("2a33affa-51b3-45e3-9a19-6fcf3e8b7171");
		Translate.setKey("829CFE911BC4FB6625622433E362BBE238BBC5C5");
		Translate
				.setClientSecret("KFdSJro4jIEgHG8Hp2royHO/VrwM2q191aFO086Rp/w=");

		// Translate.setClientId("russiantranslator");
		// Translate.setClientSecret("UDKNa9Xa/pEC2dq8gD2LkoAAocsS/3G4CnSYTSwBh7Y=");
		String translatedText = "";
		
		boolean translated = false;
		while (!translated) {
			try {
				List<String> chunks = getchunked(toTransate);
				StringBuffer buffer = new StringBuffer();
				for (String chunk : chunks) {
					try{
					translatedText = Translate.execute(chunk, Language.RUSSIAN,
							Language.ENGLISH);
					} catch (Exception e) {
						System.out.println("malformed " + chunk);
						continue;}
					buffer.append(translatedText);
					
				}
				translatedText = buffer.toString();
				if (buffer.toString().startsWith("TranslateApiException:")) {
					inLimbo();
					System.out.println(buffer.toString());
				} else {
					translated = true;
				}

				// translatedText = Translate.execute(toTransate,
				// Language.RUSSIAN, Language.ENGLISH);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.warn(e.getLocalizedMessage());
				inLimbo();
			}
		}
		System.out.println(translatedText);
		return translatedText;
	}

	// public static String translate(String toTransate) {
	// // Translate.setKey("829CFE911BC4FB6625622433E362BBE238BBC5C5");
	// // Translate.setKey("2a33affa-51b3-45e3-9a19-6fcf3e8b7171");
	// // Translate.setClientId("2a33affa-51b3-45e3-9a19-6fcf3e8b7171");
	// //
	// Translate.setClientSecret("KFdSJro4jIEgHG8Hp2royHO/VrwM2q191aFO086Rp/w=");
	//
	// Translate.setClientId("russiantranslator");
	// Translate.setClientSecret("UDKNa9Xa/pEC2dq8gD2LkoAAocsS/3G4CnSYTSwBh7Y=");
	//
	// StringBuffer buffer=new StringBuffer();
	// boolean translated = false;
	// while (!translated) {
	// try {
	// List<String> chunks=getchunked(toTransate);
	//
	// String translatedText = "";
	// for(String chunk:chunks){
	// translatedText = Translate.execute(chunk,
	// Language.RUSSIAN, Language.ENGLISH);
	// buffer.append(translatedText);
	// }
	// System.out.println("RUSSIAN -> English : " + buffer.toString());
	// translatedText= buffer.toString();
	// if (translatedText.startsWith("TranslateApiException:")) {
	// inLimbo();
	// } else {
	// translated = true;
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// logger.warn("length "+toTransate.length()+" "+e.getLocalizedMessage());
	// inLimbo();
	// }
	// }
	// return buffer.toString();
	// }

	private static List<String> getchunked(String toTransate) {
		// TODO Auto-generated method stub
		List<String> l1 = new ArrayList<String>();
		for (String chunk : Splitter.fixedLength(2000).split(toTransate)) {
			l1.add(chunk);
		}
		return l1;
	}

	public static void inLimbo() {
		try {
//			long sleeptime = 1 * 61 * 1000;
			long sleeptime = 5 * 1000;
			System.out.println(" going to sleep for -" + sleeptime);
			Thread.currentThread().sleep(sleeptime);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	}

}
